//
//  SetPersonalDataVC.m
//  XinKaiFa55like
//
//  Created by 55like on 2017/9/22.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "SetPersonalDataVC.h"
#import "SetPersonalDataCell.h"
#import "SettingTitleViewController.h"
#import "WatchCodeViewController.h"
#import "SetHeaderImageViewController.h"
#import "AddressListViewController.h"

@interface SetPersonalDataVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView * zhltableView;

//名字
@property (nonatomic,strong)NSString * realname;
//微信
@property (nonatomic,strong)NSString * wechat;
//微信二维码
@property (nonatomic,strong)NSString * wxicon;
//地址
@property (nonatomic,strong)NSString * companyaddr;
//头像地址
@property (nonatomic,strong)NSString * path;
//手机号
@property (nonatomic,strong)NSString * tel;
//邮箱
@property (nonatomic,strong)NSString * email;
//性别
@property (nonatomic,strong)NSString * gender;
//所有
@property (nonatomic,strong)NSDictionary * allData;

@end

@implementation SetPersonalDataVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.realname = @"";
    self.wechat = @"";
    self.wxicon = @"";
    self.companyaddr = @"";
    self.path = @"";
    self.tel = @"";
    self.email = @"";
    self.gender = @"";
    self.allData = [[NSDictionary alloc]init];;
    [self.zhltableView reloadData];
    [self makeData];
    
}
-(void)makeData{
    
    krequestParam
    [NetEngine createHttpAction:@"user/information" withCache:NO withParams:dictparam withMask:SVProgressHUDMaskTypeClear onCompletion:^(id resData, BOOL isCache) {
//        [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
        if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
            
            
            self.allData = resData[@"data"];
            self.realname = [self.allData valueForJSONStrKey:@"realname"];
            self.wechat = [self.allData valueForJSONStrKey:@"wechat"];
            self.wxicon = [self.allData valueForJSONStrKey:@"wxicon"];
            self.companyaddr = [NSString stringWithFormat:@"%@%@%@%@",[self.allData valueForJSONStrKey:@"province"],[self.allData valueForJSONStrKey:@"city"],[self.allData valueForJSONStrKey:@"area"],[self.allData valueForJSONStrKey:@"address"]];
            self.path = [self.allData valueForJSONStrKey:@"path"];
            self.tel = [self.allData valueForJSONStrKey:@"tel"];
            self.email = [self.allData valueForJSONStrKey:@"email"];
            self.gender = [self.allData valueForJSONStrKey:@"gender"];
            [self.zhltableView reloadData];
            
            
            
        }else{
            
            
            
            
            
            
        }
        
    } onError:^(NSError *error) {
        
    }];
    
    
    
    
    
}
static NSString * SetPersonalDataCellID  = @"SetPersonalDataCellID";

-(UITableView *)zhltableView{
    
    if (!_zhltableView) {
        
        UITableView * zhltableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:UITableViewStyleGrouped];
        zhltableView.delegate = self;
        zhltableView.dataSource = self;
        zhltableView.backgroundColor = rgb(242, 242, 242);
        zhltableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        zhltableView.showsVerticalScrollIndicator = NO;
        zhltableView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:zhltableView];
        [zhltableView registerClass:[SetPersonalDataCell class] forCellReuseIdentifier:SetPersonalDataCellID];
        _zhltableView = zhltableView;
    }
    _zhltableView.tableHeaderView = self.tableivewHeaderView;
    
    
 
    
    return _zhltableView;
}
-(UIView *)tableivewHeaderView{
    
    UIView * tableivewHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,kScreenWidth,60 * kScreenWidth/320)];
    tableivewHeaderView.backgroundColor = [UIColor whiteColor];
    UILabel * tittle = [RHMethods labelWithFrame:CGRectMake(10, 0, 100, tableivewHeaderView.frameHeight) font:Font(15) color:rgbTitleDeepGray text:@"头像"];
    [tableivewHeaderView addSubview:tittle];
    
    UIImageView * icon = [[UIImageView alloc]initWithFrame: CGRectMake(kScreenWidth - 10 - 8, (tableivewHeaderView.frameHeight - 29/2)/2, 16/2, 29/2)];
    icon.image = [UIImage imageNamed:@"xkf_searrighticon"];
    [tableivewHeaderView addSubview:icon];
    
    UIImageView * headerImage = [[UIImageView alloc]initWithFrame:CGRectMake(X(icon) - 10 - (tableivewHeaderView.frameHeight - 16), 8, tableivewHeaderView.frameHeight - 16, tableivewHeaderView.frameHeight - 16)];
    [headerImage imageWithURL:self.path defaultImage:@"xkf_txmr"];
    headerImage.contentMode = UIViewContentModeScaleAspectFill;
    headerImage.clipsToBounds = YES;
    headerImage.layer.cornerRadius = 5;
    [tableivewHeaderView addSubview:headerImage];
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, tableivewHeaderView.frameHeight, 10, 2 )];
    view.backgroundColor = [UIColor whiteColor];
    [tableivewHeaderView addSubview:view];
    
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, tableivewHeaderView.frameHeight)];
    [button addTarget:self action:@selector(setImage) forControlEvents:UIControlEventTouchUpInside];
    [tableivewHeaderView addSubview:button];
//
    
    return tableivewHeaderView;
}
-(void)setImage{
    EMWeakSelf(weakSelf);
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setValue:self.path forKey:@"path"];
    [dic setObject:^(){
        
        [weakSelf makeData];
        
    } forKey:@"block"];
    
    [self pushController:[SetHeaderImageViewController class] withInfo:dic withTitle:@"个人头像"];
    
    
}
#pragma mark -  tableview代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return self.titleArray.count;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    
    return [self.titleArray[section] count];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return 44;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
         return CGFLOAT_MIN;
    }
    
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section    {
    
    
    
    return CGFLOAT_MIN;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    UIView * headerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    
    
    return headerview;
    
    
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    
    UIView * footerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 0)];
    footerview.backgroundColor = [UIColor whiteColor];
    
    
    
    return footerview;
    
    
}
#pragma mark  cell处理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SetPersonalDataCell * cell = [tableView dequeueReusableCellWithIdentifier:SetPersonalDataCellID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSString * titleStr = self.titleArray[indexPath.section][indexPath.row];
    NSString * detaislStr = self.details[indexPath.section][indexPath.row];
    
    cell.titlelab.frame = CGRectMake(10, 0, [titleStr widthWithFont:15], cell.frameHeight);
    cell.titlelab.text = titleStr;
    cell.icon.frame = CGRectMake(kScreenWidth - 10 - 8, (cell.frameHeight - 29/2)/2, 16/2, 29/2);
    cell.icon.image = [UIImage imageNamed:@"Righticon070116"];
    cell.imageview.frame = CGRectMake(0, 0, 0, 0);
    cell.deslab.text = detaislStr;
    cell.deslab.frame = CGRectMake(XW(cell.titlelab) + 10, 0, cell.icon.frameX - XW(cell.titlelab) - 20, cell.frameHeight);
    cell.icon.hidden = NO;
    cell.imageview.hidden = YES;
    cell.deslab.hidden = NO;
    
    if ([titleStr isEqualToString:@"二维码"]) {
        
        cell.imageview.hidden = NO;
        cell.imageview.frame = CGRectMake(X(cell.icon) - 10 - 17, (cell.frameHeight - 17)/2, 17, 17);
        cell.imageview.image = [UIImage imageNamed:@"xkf_ewm"];
        cell.deslab.hidden = YES;
        
    }else if ([titleStr isEqualToString:@"微信"]){
        
//        cell.icon.hidden = YES;
//        cell.deslab.frameWidth = kScreenWidth - 10 - XW(cell.titlelab) - 10;
        
    }
    
    
    
    
    
    
    return cell;
    
}
#pragma mark  选中cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    EMWeakSelf(weakSelf);
    NSLog(@"%@",indexPath);
    NSString * str = self.titleArray[indexPath.section][indexPath.row];
    NSString * desic = self.details[indexPath.section][indexPath.row];
    if ([str isEqualToString:@"名字"] || [str isEqualToString:@"电话"]||[str isEqualToString:@"邮箱"]||[str isEqualToString:@"微信"]) {
        
        NSMutableDictionary * dic  = [[NSMutableDictionary alloc]init];
        [dic setValue:desic forKey:@"text"];
        [dic setValue:str forKey:@"title"];
        [dic setObject:^(){
            
            [weakSelf makeData];
            
        } forKey:@"block"];
        [self pushController:[SettingTitleViewController class] withInfo:dic withTitle:str withOther:nil];
        
        
    }else if([str isEqualToString:@"二维码"]){
        
        NSMutableDictionary * dic  = [[NSMutableDictionary alloc]init];
        [dic setObject:^(){
            
            [weakSelf makeData];
            
        } forKey:@"block"];
        [self pushController:[WatchCodeViewController class] withInfo:self.allData withTitle:str withOther:dic];
        
        
        
    }else if ([str isEqualToString:@"地址"]){
        
        
        
        NSMutableDictionary * dic  = [[NSMutableDictionary alloc]init];
        [dic setObject:^(){
            
            [weakSelf makeData];
            
        } forKey:@"block"];
        [self pushController:[AddressListViewController class] withInfo:self.allData withTitle:str withOther:dic];
    }
//
}
-(NSArray *)titleArray{
    
    
    return @[@[@"名字",@"电话",@"微信",@"二维码",@"邮箱"],@[@"地址"]];
}
-(NSArray *)details{
    

    return @[@[self.realname,self.tel,self.wechat,@"",self.email],@[self.companyaddr]];
}
@end
